﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http;
using System.Web.Http;
using System.Net.Http.Formatting;
using vncom;
using vncom.Models;
using System.Web.Security;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace vncom.Controllers
{
    public class MemberServicesController : ApiController
    {
        #region ImageAPI
        //
        // GET: /MemberServices/
        [Authorize]
        [HttpGet]
        public HttpResponseMessage GetAllPictures()
        {
            var username = Membership.GetUser().UserName;
            int y = 0;
            IContentNegotiator config = this.Configuration.Services.GetContentNegotiator();
            ContentNegotiationResult result = config.Negotiate(
                typeof(List<SerializePicture>), this.Request, this.Configuration.Formatters);
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);

            var listPic = DataProvider.SelectStoreProcedure("GetAllPicByUser", listPara);
            List<SerializePicture> serial = new List<SerializePicture>();
            foreach (DataRow item in listPic.Rows)
            {
                //DataColumn col1 = listProvince.Columns["ProvinceID"];
                SerializePicture newObj = new SerializePicture()
                {
                    PicID = item["PicID"].ToString(),
                    FileName = item["FileName"].ToString(),
                    Description = item["Description"].ToString()
                };
                serial.Add(newObj);
            }
            return new HttpResponseMessage()
            {
                Content = new ObjectContent<List<SerializePicture>>(serial, result.Formatter, result.MediaType.MediaType)
            };
        }

        [Authorize]
        [HttpGet]
        public HttpResponseMessage DeletePicture(string id)
        {
            var username = Membership.GetUser().UserName;

            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);
            SqlParameter p2 = new SqlParameter("@pic_id", SqlDbType.VarChar);
            p2.Value = id;
            listPara.Add(p2);
            SqlParameter resultProc = new SqlParameter("@result", SqlDbType.VarChar);
            resultProc.Value = "0";

            var cmd = DataProvider.OutPutProc("DeletePicByID", listPara, resultProc);
            
            if(resultProc.Value.ToString()=="1")
            {
                File.Delete(HttpContext.Current.Server.MapPath("/MemberPictures/" + id));
                return new HttpResponseMessage()
                {
                    Content = new StringContent("ok")
                };
            }
            else if (resultProc.Value.ToString() == "2")
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("not found")
                };
            }
            else
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("error")
                };
            }
        }

        [Authorize]
        [HttpPost]
        public HttpResponseMessage UploadPicture(Api_File file)
        {

            if (file.MediaType == "image/jpeg" || file.MediaType == "image/png" || file.MediaType == "image/gif")
            {
                string time = vncom.GlobalHelper.N_ConvertDateTimeToTimestamp(DateTime.Now).ToString();
                var r = new Random();
                int random = r.Next(0, 999);
                string filename = time + random.ToString() + "-" + file.FileName;
                System.IO.FileStream serverfile = System.IO.File.Create(HttpContext.Current.Server.MapPath("/MemberPictures/" + filename));
                serverfile.Write(file.Buffer, 0, file.Buffer.Length);
                serverfile.Close();
                var username = Membership.GetUser().UserName;

                List<SqlParameter> listPara = new List<SqlParameter>();
                SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
                p1.Value = username;
                listPara.Add(p1);
                SqlParameter p2 = new SqlParameter("@file_name", SqlDbType.VarChar);
                p2.Value = filename;
                listPara.Add(p2);
                SqlParameter p3 = new SqlParameter("@description", SqlDbType.NVarChar);
                p3.Value = "";
                listPara.Add(p3);
                var cmd = DataProvider.SelectStoreProcedure("NewPic", listPara);
            }
            else
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("Lỗi định đang file")
                };
            }

            return new HttpResponseMessage()
            {
                Content = new StringContent("Thành công")
            };
        }
        #endregion

        #region FlashAPI

        [Authorize]
        [HttpPost]
        public HttpResponseMessage UploadFlash(Api_File file)
        {

            if (file.MediaType == "application/x-shockwave-flash")
            {
                string time = vncom.GlobalHelper.N_ConvertDateTimeToTimestamp(DateTime.Now).ToString();
                var r = new Random();
                int random = r.Next(0, 999);
                string filename = time + random.ToString() + "-" + file.FileName;
                System.IO.FileStream serverfile = System.IO.File.Create(HttpContext.Current.Server.MapPath("/MemberFlashes/" + filename));
                serverfile.Write(file.Buffer, 0, file.Buffer.Length);
                serverfile.Close();
                var username = Membership.GetUser().UserName;

                List<SqlParameter> listPara = new List<SqlParameter>();
                SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
                p1.Value = username;
                listPara.Add(p1);
                SqlParameter p2 = new SqlParameter("@fname", SqlDbType.VarChar);
                p2.Value = filename;
                listPara.Add(p2);
                var cmd = DataProvider.SelectStoreProcedure("NewFlash", listPara);
            }
            else
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("Lỗi định đang file")
                };
            }

            return new HttpResponseMessage()
            {
                Content = new StringContent("Thành công")
            };
        }

        [Authorize]
        [HttpGet]
        public HttpResponseMessage GetAllFlash()
        {
            var username = Membership.GetUser().UserName;
            int y = 0;
            IContentNegotiator config = this.Configuration.Services.GetContentNegotiator();
            ContentNegotiationResult result = config.Negotiate(
                typeof(List<SerializeFlash>), this.Request, this.Configuration.Formatters);
            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);

            var listPic = DataProvider.SelectStoreProcedure("GetAllFlashByUser", listPara);
            List<SerializeFlash> serial = new List<SerializeFlash>();
            foreach (DataRow item in listPic.Rows)
            {
                //DataColumn col1 = listProvince.Columns["ProvinceID"];
                SerializeFlash newObj = new SerializeFlash()
                {
                    FlashID = item["FlashID"].ToString(),
                    FileName = item["FileName"].ToString()
                };
                serial.Add(newObj);
            }
            return new HttpResponseMessage()
            {
                Content = new ObjectContent<List<SerializeFlash>>(serial, result.Formatter, result.MediaType.MediaType)
            };
        }

        [Authorize]
        [HttpGet]
        public HttpResponseMessage DeleteFlash(string id)
        {
            var username = Membership.GetUser().UserName;

            List<SqlParameter> listPara = new List<SqlParameter>();
            SqlParameter p1 = new SqlParameter("@username", SqlDbType.NVarChar);
            p1.Value = username;
            listPara.Add(p1);
            SqlParameter p2 = new SqlParameter("@fname", SqlDbType.VarChar);
            p2.Value = id;
            listPara.Add(p2);
            SqlParameter resultProc = new SqlParameter("@result", SqlDbType.VarChar);
            resultProc.Value = "0";

            var cmd = DataProvider.OutPutProc("DeleteFlash", listPara, resultProc);

            if (resultProc.Value.ToString() == "1")
            {
                File.Delete(HttpContext.Current.Server.MapPath("/MemberFlashes/" + id));
                return new HttpResponseMessage()
                {
                    Content = new StringContent("ok")
                };
            }
            else if (resultProc.Value.ToString() == "2")
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("not found")
                };
            }
            else
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("error")
                };
            }
        }

        #endregion

        #region FileAPI

        #endregion

    }
}
